<?php
namespace jy_feieyun;
defined('BY_JYA') or exit('error');
/*
	打印机http://help.feieyun.com/document.php
*/
class jinyunzn extends \plugin{
	private $user;
	private $ukey;
	/*
		接口类实例化参数
			$user	用户user
			$ukey	用户密钥
	*/
	public function __construct($data=null){
		if(!$data['user'] || !$data['ukey']){
			exi('接口参数错误','error');
		}
		$this->user=$data['user'];
		$this->ukey=$data['ukey'];
	}
	/*
		检查指定打印机是否已激活
		参数说明：
			$seri_num	序列号
	*/
	public function queryPrinterStatus($sn){
		$time = time();         //请求时间
		$msgInfo = array(
		  'user'=>$this->user,
		  'stime'=>$time,
		  'sig'=>$this->get_sign($time),
		  'apiname'=>'Open_queryPrinterStatus',
		  'sn'=>$sn
		);
		return $this->request($msgInfo);
	}
	/**
   * [批量添加打印机接口 Open_printerAddlist]
   * @param  [string] $printerContent [打印机的sn#key]
   * @return [string]                 [接口返回值]
   */
  function printerAddlist($printerContent){
    $time = time();         //请求时间
    $msgInfo = array(
      'user'=>$this->user,
	  'stime'=>$time,
	  'sig'=>$this->get_sign($time),
      'apiname'=>'Open_printerAddlist',
      'printerContent'=>$printerContent
    );
    return $this->request($msgInfo);
  }
  /**
   * [打印订单接口 Open_printMsg]
   * @param  [string] $sn      [打印机编号sn]
   * @param  [string] $content [打印内容]
   * @param  [string] $times   [打印联数]
   * @return [string]          [接口返回值]
   */
  public function printMsg($sn,$content,$print_type=0){
    $time = time();         //请求时间
    $msgInfo = array(
      'user'=>$this->user,
	  'stime'=>$time,
	  'sig'=>$this->get_sign($time),
      'apiname'=>$print_type?'Open_printLabelMsg':'Open_printMsg',
      'sn'=>$sn,
      'content'=>$content,
      'times'=>$times?:1//打印次数
    );
    return $this->request($msgInfo);
  }

  /**
   * [标签机打印订单接口 Open_printLabelMsg]
   * @param  [string] $sn      [打印机编号sn]
   * @param  [string] $content [打印内容]
   * @param  [string] $times   [打印联数]
   * @return [string]          [接口返回值]
   */
  public function printLabelMsg($sn,$content,$times=1){
    $time = time();         //请求时间
    $msgInfo = array(
      'user'=>$this->user,
	  'stime'=>$time,
	  'sig'=>$this->get_sign($time),
      'apiname'=>'Open_printLabelMsg',
      'sn'=>$sn,
      'content'=>$content,
      'times'=>$times//打印次数
    );
    return $this->request($msgInfo);
  }
	/**
   * [修改打印机信息接口 Open_printerEdit]
   * @param  [string] $sn       [打印机编号]
   * @param  [string] $name     [打印机备注名称]
   * @param  [string] $phonenum [打印机流量卡号码,可以不传参,但是不能为空字符串]
   * @return [string]           [接口返回值]
   */
	public function printerEdit($sn,$name,$phonenum=''){
		$time = time();         //请求时间
		$msgInfo = array(
		  'user'=>$this->user,
		  'stime'=>$time,
		  'sig'=>$this->get_sign($time),
		  'apiname'=>'Open_printerEdit',
		  'sn'=>$sn,
		  'name'=>$name,
		);
		if($phonenum){
			$msgInfo['phonenum']=$phonenum;
		}
		return $this->request($msgInfo);
	 }
	/*
		读取用户已激活打印机列表
		参数说明：
			无
	*/
	public function get_list(){
		return array();
	}
	/*
		发起打印请求
		参数说明：
			$seri_num	打印机序列号
			$print_data		打印内容
			$print_type		打印类型，可不填
	*/
	public function printing($seri_num,$print_data,$print_type=0,$nowait=false){
		return $this->printMsg($seri_num,$print_data,$print_type);
	}
	/*
		整理请求数据
		参数说明：
			$action	请求类型，可能的值：print表示打印，list表示获取用户打印机列表，check表示检查指定打印机是否可用。
			$params	业务参数，和请求类型有关
	*/
	public function request($post,$nowait=false){
		return $this->post($post,$nowait);
	}
	/*
		向云平台发起请求
	*/
	public function post($post,$nowait=false){
		$api_url='http://api.feieyun.cn/Api/Open/';
		if($nowait){
			$timeout=1;
		}else{
			$timeout=60;
		}
		
		$ch = curl_init($api_url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
		curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
		$res=curl_exec($ch);
		$res=@json_decode($res,true);
		return $res;
	}
	/*
		根据密钥生成签名
	*/
	public function get_sign($time){
		 return sha1($this->user.$this->ukey.$time);//公共参数，请求公钥
	}
	
}
